﻿@page "/"

@using ServerApp.Models
@using ServerApp.Services
@inject WeatherForecastService ForecastService

<TelerikGrid Height="550px" FilterMode="@GridFilterMode.FilterMenu"
             Sortable="true" Pageable="true" Groupable="true" PageSize="20"
             Data=@GridData TotalCount=@Total OnRead=@ReadItems>
    <GridColumns>
        <GridColumn Field="Id" FieldType="@(typeof(int))" Title="Id" Width="100px" Groupable="false" />
        <GridColumn Field="Date" FieldType="@(typeof(DateTime))">
            <Template>
                @((context as WeatherForecast).Date.ToString("dddd, dd MMM yyyy"))
            </Template>
        </GridColumn>
        <GridColumn Field="TemperatureC" FieldType="@(typeof(int))" Title="Temp. C" />
        <GridColumn Field="TemperatureF" FieldType="@(typeof(int))" Title="Temp. F" />
        <GridColumn Field="Summary" FieldType="@(typeof(string))" />
    </GridColumns>
</TelerikGrid>

@code {
    List<object> GridData { get; set; }
    public int Total { get; set; } = 0;

    protected async Task ReadItems(GridReadEventArgs args)
    {
        // we pass the request to the service, and there Telerik DataSource Extension methods will shape the data
        var datasourceResult = await ForecastService.GetForecastListAsync(args.Request);

        GridData = datasourceResult.Data.Cast<object>().ToList();

        Total = datasourceResult.Total;

        StateHasChanged();
    }

    // for brevity, CUD operations are not implemented, only Read
}